package com.hb.trade.api.routine.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import com.hb.core.pojo.JSONObjectProperty;
import com.hb.core.pojo.center.sys.dto.NewsCenterDTO;
import com.hb.core.pojo.enums.*;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.api.routine.client.ImClient;
import com.hb.trade.client.center.NewsCenterClient;
import com.hb.trade.client.project.ProjectClient;
import com.hb.trade.client.project.SectionClient;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.websocket.SocketMessageEntity;
import org.fzzn.core.util.Enums;

import java.util.List;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/02/25
 */
public class SendNewsUtil {
    private static final JSONObject jsonObject = new JSONObject();

    private static final SocketMessageEntity messageEntity = SocketMessageEntity.getInstance();

    /**
     *
     * @Description: 给招标人发送审核消息
     * @Author: zhongdl
     * @Date:2022/2/25 11:42
     * @param sectionId:标段id
     * @param newsTypeEnum:消息类型
     * @param newsTypeTwoEnum:消息二级类型
     * @param filedName:字段名称
     **/
    public static void sendTendereeNews(Long sectionId,NewsTypeEnum newsTypeEnum,NewsTypeTwoEnum newsTypeTwoEnum,String filedName,String route){
        sendTendereeNews(sectionId,newsTypeEnum,newsTypeTwoEnum,filedName,route,null);
    }
    /**
     *
     * @Description: 给招标人发送审核消息
     * @Author: zhongdl
     * @Date:2022/2/25 11:42
     * @param sectionId:标段id
     * @param newsTypeEnum:消息类型
     * @param newsTypeTwoEnum:消息二级类型
     * @param filedName:字段名称
     **/
    public static void sendTendereeNews(Long sectionId, NewsTypeEnum newsTypeEnum, NewsTypeTwoEnum newsTypeTwoEnum, String filedName, String filedStatus, String route, JSONObjectProperty query){
        SectionClient sectionClient = SpringUtil.getBean(SectionClient.class);
        ProjectClient projectClient = SpringUtil.getBean(ProjectClient.class);
        NewsCenterClient newsCenterClient = SpringUtil.getBean(NewsCenterClient.class);

        BidSectionDTO bidSectionDTO = new BidSectionDTO();
        bidSectionDTO.setSectionId(sectionId);
        Message<BidSectionDTO> bidSectionDTOMessage = sectionClient.getBidSection(bidSectionDTO);
        if (bidSectionDTOMessage.isFail() || bidSectionDTOMessage.getData() == null){
            return;
        }
        bidSectionDTO = bidSectionDTOMessage.getData();

        AbstractProject paramAbstractProject = new AbstractProject();
        paramAbstractProject.setId(bidSectionDTO.getProjectId());
        Message<AbstractProject> projectDetail = projectClient.projectDetail(paramAbstractProject);
        if (projectDetail.isFail() || projectDetail.getData() == null){
            return;
        }
        AbstractProject abstractProject = projectDetail.getData();
        NewsCenterDTO newsCenterDTO = new NewsCenterDTO();
        newsCenterDTO.setNewsType(newsTypeEnum.getCode());
        newsCenterDTO.setNewsTypeTwo(newsTypeTwoEnum.getCode());
        if (StrUtil.isNotEmpty(filedName)){
            newsCenterDTO.setFieldName(filedName);
        }
        bidSectionDTO.to(newsCenterDTO);
        newsCenterDTO.setId(null);
        newsCenterDTO.setReceiverIds(CollUtil.list(false,abstractProject.getUserId()));
        newsCenterDTO.setSectionId(sectionId);
        newsCenterDTO.setRoute(route);
        newsCenterDTO.setProjectId(abstractProject.getId());
        newsCenterDTO.setProjectName(abstractProject.getProjectName());
        newsCenterDTO.setSectionName(bidSectionDTO.getSectionName());
        if (StrUtil.isNotEmpty(filedStatus)){
            newsCenterDTO.setFieldStatus(filedStatus);
        }
        if (query != null){
            newsCenterDTO.setRouteQuery(query);
        }
        newsCenterClient.insertNewsCenter(newsCenterDTO);
        pushWebSocket();
    }

    /**
     *
     * @Description: 给招标人发送审核消息
     * @Author: zhongdl
     * @Date:2022/2/25 11:42
     * @param sectionId:标段id
     * @param newsTypeEnum:消息类型
     * @param newsTypeTwoEnum:消息二级类型
     * @param filedName:字段名称
     **/
    public static void sendTendereeNews(Long sectionId,NewsTypeEnum newsTypeEnum,NewsTypeTwoEnum newsTypeTwoEnum,String filedName,String route,JSONObjectProperty query){
        sendTendereeNews(sectionId,newsTypeEnum,newsTypeTwoEnum,filedName,null,route,query);
    }
    /**
     *
     * @Description: 给投标人发送审核消息
     * @Author: zhongdl
     * @Date:2022/2/25 11:43
     * @param sectionId:标段id
     * @param newsTypeEnum:消息类型
     * @param newsTypeTwoEnum:消息二级类型
     * @param filedName:字段名称
     * @param filedStatus:状态(已通过/已驳回)
     * @param route:页面路由
     * @param receiverIds:接收人的id
     **/
    public static void sendBiddereeNews(Long sectionId, NewsTypeEnum newsTypeEnum, NewsTypeTwoEnum newsTypeTwoEnum, String filedName, String filedStatus, String  route,List<Long> receiverIds){
        SectionClient sectionClient = SpringUtil.getBean(SectionClient.class);
        ProjectClient projectClient = SpringUtil.getBean(ProjectClient.class);
        NewsCenterClient newsCenterClient = SpringUtil.getBean(NewsCenterClient.class);

        BidSectionDTO bidSectionDTO = new BidSectionDTO();
        bidSectionDTO.setSectionId(sectionId);
        Message<BidSectionDTO> bidSectionDTOMessage = sectionClient.getBidSection(bidSectionDTO);
        if (bidSectionDTOMessage.isFail() || bidSectionDTOMessage.getData() == null){
            return;
        }
        bidSectionDTO = bidSectionDTOMessage.getData();
        AbstractProject paramAbstractProject = new AbstractProject();
        paramAbstractProject.setId(bidSectionDTO.getProjectId());
        Message<AbstractProject> projectDetail = projectClient.projectDetail(paramAbstractProject);
        if (projectDetail.isFail() || projectDetail.getData() == null){
            return;
        }
        AbstractProject abstractProject = projectDetail.getData();
        NewsCenterDTO newsCenterDTO = new NewsCenterDTO();
        newsCenterDTO.setNewsType(newsTypeEnum.getCode());
        newsCenterDTO.setNewsTypeTwo(newsTypeTwoEnum.getCode());
        if (StrUtil.isNotEmpty(filedName)){
            newsCenterDTO.setFieldName(filedName);
        }
        if (StrUtil.isNotEmpty(filedStatus)){
            newsCenterDTO.setFieldStatus(filedStatus);
        }
        bidSectionDTO.to(newsCenterDTO);
        newsCenterDTO.setId(null);
        newsCenterDTO.setReceiverIds(receiverIds);
        newsCenterDTO.setRoute(route);
        newsCenterDTO.setSectionId(sectionId);
        newsCenterDTO.setProjectId(abstractProject.getId());
        newsCenterDTO.setProjectName(abstractProject.getProjectName());
        newsCenterDTO.setSectionName(bidSectionDTO.getSectionName());
        newsCenterClient.insertNewsCenter(newsCenterDTO);
        pushWebSocket();
    }

    /**
     *
     * @Description:发送消息根据费用类型获得前端路由
     * @Author: zhongdl
     * @Date:2022/2/25 14:01
     * @param feeType:费用类型
     * @return: {@link String }
     **/
    public static String getRoute(Integer feeType){
        String route = "";
        FeeTypeEnum feeTypeEnum = Enums.of(FeeTypeEnum.class, feeType);
        switch (feeTypeEnum) {
            case BID_PRO: // 投标保证金
                route = "/console/deposit";
                break;
            case PER_PRO: // 履约保证金
                route = "/console/bond";
                break;
            case SIGN_UP: // 报名费
            case DOC_BOOK: // 标书管理费
                route = "/console/tenderManage";
                break;
            case SYS_USE: // 系统使用费
                route = "/console/systemFeeManage";
                break;
            case OTHER: // 其他商品
            default:
                route = "";

        }
        return route;
    }

    public static String getNoticeRoute(Integer noticeNature){
        String route = "/console/noticelist";
        NoticeNatureEnum feeTypeEnum = Enums.of(NoticeNatureEnum.class, noticeNature);
        switch (feeTypeEnum) {
            case FIRST: // 首次公告
            case CHANGE: // 变更公告
            case DELAY: // 延期公告
            case SUP: // 补充
            case TERMIN: // 终止
            case AGAIN: // 再次
            case LIMIT: // 控制价
            case FAILURE: // 控制价
            default:
                route = "/console/noticelist";

        }
        return route;
    }


    /**
     *
     * @Description:发送消息根据费用类型获得前端路由
     * @Author: zhongdl
     * @Date:2022/2/25 14:01
     * @param feeType:费用类型
     * @return: {@link String }
     **/
    public static String getTendereeRoute(Integer feeType){
        String route = "";
        FeeTypeEnum feeTypeEnum = Enums.of(FeeTypeEnum.class, feeType);
        switch (feeTypeEnum) {
            case BID_PRO: // 投标保证金
                route = "/projectconsole";
                break;
            case PER_PRO: // 履约保证金
                route = "/projectconsole";
                break;
            case SIGN_UP: // 报名费
            case DOC_BOOK: // 标书管理费
                route = "/projectconsole";
                break;
            case SYS_USE: // 系统使用费
                route = "/projectconsole";
                break;
            case OTHER: // 其他商品
            default:
                route = "";

        }
        return route;
    }

    public static JSONObjectProperty getTendereeQuery(Integer feeType){
        JSONObjectProperty jsonEntity = new JSONObjectProperty();
        FeeTypeEnum feeTypeEnum = Enums.of(FeeTypeEnum.class, feeType);
        switch (feeTypeEnum) {
            case BID_PRO: // 投标保证金
                jsonEntity.putOpt("activeName","depositFeeManage");
                break;
            case PER_PRO: // 履约保证金
                jsonEntity.putOpt("activeName","PerformanceBond");

                break;
            case SIGN_UP: // 报名费
            case DOC_BOOK: // 标书管理费
                jsonEntity.putOpt("activeName","tenderFeeManage");
                break;
            case SYS_USE: // 系统使用费
                jsonEntity.putOpt("activeName","systemFeeManage");
                break;
            case OTHER: // 其他商品
            default:
                 break;

        }
        return jsonEntity;
    }

    public static void pushWebSocket(){
        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                ImClient imClient = SpringUtil.getBean(ImClient.class);

                //构造WebSocket发送消息体
                messageEntity.setInfoType(TradeWebsocketMsgEnum.MESSAGE_CENTER.getCode());
                messageEntity.setTypeName(TradeWebsocketMsgEnum.MESSAGE_CENTER.getShowName());
                jsonObject.putOpt("sectionId","test");
                messageEntity.setContentData(jsonObject);
                //执行发送操作
                imClient.pushMessageCenter(messageEntity);

            }
        });

    }
}
